Global Lua Modules/I18n
}} I18n-lua is a library for message storage as Lua datastores. It enables message separation from modules and templates. It has support for handling language fallbacks. This module is a Lua port of I18n-js. TODO: Support for Lua i18n submodules in I18n-js editor. Usage I18n datastore To use the module, you need to set up your messages in the appropriate place and correct format. The format of your messages should be as follows: return { "en" = { "message-name" = "value" }, "pl" = { "message-name" = "value" } } Messages can have arguments for substitution in templates/modules - these should be specified in the form $n where n is a integer greater than 0, e.g, "Hello, $1, my name is $2". Fetching the messages The message loader will expect your messages to be in a Dev Wiki page such as Module:PAGENAME/i18n, where PAGENAME should be the name of your module. Alternatively, an absolute path can be supplied (if PAGENAME contains a ":"). Once you've set up the messages, there are two ways to fetch them. 1. Import the module into a Lua module and create a datastore instance using the i18n.loadMessages method: local i18nd = require('Dev:I18n').loadMessages('PAGENAME', 'PAGENAME2') -- If a PAGENAME contains a ":" the path will be treated as absolute! 2. Fetch messages directly within your template: | Parameters = }} Use in templates ;i18n.getLang :This fetches the appropriate language code to describe user preference. :Allows validation of a template's language code through uselang parameter. :Usage (uselang): code}} ;i18n.getMsg :This fetches individual, localized messages from a datastore by key. :Allows returning messages in a specific language code through uselang parameter. :Allows message substitution by passing through extra numbered parameters. :Usage (uselang): code}} Message usage in Lua When we have created our i18nd datastore instance, it will control access to your individual messages as well as the language it tries to translate them into. It defines the following methods: ;i18nd:msg(key, ...) * Returns localised messages from the datastore corresponding to the key string. * Allows optional substitution of $n parameters, using extra argument strings * Alternative table-based syntax: i18nd:msg { key = 'message-name', lang = , args = {...}, sources = {...} } * Configuration parameters: ** lang - temporary language (equivalent to i18nd:inLang) ** args - arguments to substitute (equivalent to '...' above) ** sources - temporary language (equivalent to i18nd:fromSource) Datastore and message configuration Temporary source handling By default, the messages are fetched from the datastore in the same order of priority as i18n.loadMessages. However, it is possible to override this: ;i18nd:fromSource(...) :Takes a subset of source names and limits i18nd:msg to these temporarily. Default language handling '''Note:' the methods marked *''' are chainable. ;i18nd:getLang(): * Gets the default language of the datastore messages. ;i18nd:useLang(code)*: * Set the default language to code, e.g. 'pt'. * By default, the language will be the value of wgUserLanguage. ;i18nd:useContentLang()*: * Set the default language to the value of wgContentLanguage. ;i18nd:useUserLang()*: * Set the default language to the value of wgUserLanguage. Note that Scribunto only registers wgUserLanguage when the module is #invoked. Temporary language overrides '''Note: the methods marked * are chainable. ;i18nd:inLang(code)*: * Same as i18nd:useLang but only sets the language the next i18nd:msg call will use. ;i18nd:inContentLang()*: * Same as i18nd:useContentLang but only sets the language the next i18nd:msg call will use. ;i18nd:inUserLang()*: * Same as i18nd:useUserLang but only sets the language the next i18nd:msg call will use. Changelog . ;November 19, 2018 (v1.3.2) :MACH-59330: Fix subpage test in i18n.getLang. ;November 20, 2018 (v1.3.2) :MACH-59330: Remove support for qqx detection (broken). | padding = 1em }} See also * I18n guide * I18n-js